/*
 * Copyright (c) 2022.版权所有高金磊
 */

package 领扣算法.AA中等题.重排数字的最小值;

import java.util.Arrays;

public class Main {
    public long smallestNumber(long num) {
        int target=num<0?-1:1;
        num*=target;
        char[] middle=(""+num).toCharArray();
        Integer[] data=new Integer[middle.length];
        for (int i = 0; i <middle.length ; i++) {
            data[i]=middle[i]-'0';
        }
        Arrays.sort(data,(a,b)->((a-b)*target));
        if (data[0]==0){
            for (int i = 1; i < data.length; i++) {
                if (data[i]!=0){
                    data[0]=data[i];
                    data[i]=0;
                    break;
                }
            }
        }
        long res=0;
        for (Integer datum : data) {
            res*=10;
            res+=datum;
        }
        return res*target;
    }
}
